﻿#ifndef __HTTPMANGER_H__
#define __HTTPMANGER_H__

#ifndef QT_NETWORK_LIB
#   error("Plwaer add network in pro file")
#endif

#include "Record.h"

// C++ lib import
#include <functional>

// Qt lib import
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkInterface>
#include <QNetworkAddressEntry>

QNetworkAddressEntry getNetworkAddressEntry();

QPair< QNetworkAddressEntry, QNetworkInterface > getNetworkAddressEntryWithNetworkInterface(const bool &ridVm = true);

QString getHostName();

class HTTP
{
public:
    inline QNetworkAccessManager &manage() { return manage_; }


    bool get(const QNetworkRequest &request, QByteArray &target, const int &timeout = 30000);

    void get(const QNetworkRequest &request,
             const std::function< void(const QByteArray &data) > &onFinished,
             const std::function< void(const QNetworkReply::NetworkError &code) > &onError,
             const int &timeout = 30000);

    bool post(const QNetworkRequest &request, const QByteArray &appendData, QByteArray &target, const int &timeout);

    void post(const QNetworkRequest &request,
              const QByteArray &appendData,
              const std::function< void(const QByteArray &data) > &onFinished,
              const std::function< void(const QNetworkReply::NetworkError &code) > &onError,
              const int &timeout = 30000);


    static QPair< bool, QByteArray > get(const QString &url, const int &timeout = 30000);

    static QPair< bool, QByteArray > get(const QNetworkRequest &request, const int &timeout = 30000);

    static QPair< bool, QByteArray > post(const QString &url, const QByteArray &appendData, const int &timeout = 30000);

    static QPair< bool, QByteArray > post(const QNetworkRequest &request, const QByteArray &appendData, const int &timeout = 30000);

private:
    void handle(QNetworkReply *reply, const int &timeout,
                const std::function< void(const QByteArray &data) > &onFinished,
                const std::function< void(const QNetworkReply::NetworkError &code) > &onError,
                const std::function< void() > &onTimeout);

private:
    QNetworkAccessManager manage_;
};


/***********************************************************************************************************************************/
class HttpManger : public QObject
{
	Q_OBJECT

public:
	HttpManger(QObject *parent = 0);
	~HttpManger();

public:
    QDateTime getTime();
	bool exec(const QString & SQL, QString & Msg);
	bool execBatch(const QStringList & SQL, QString & Msg);
	bool select(const QString & SQL, QRecord & dsRecord, QString & Msg);

protected:
	void initData();
	bool parse(const QByteArray & data, QString & strMsg);
	bool serialize(const QByteArray & data, QRecord & dsRecord, QString & strMsg);

protected:
	static int iType;
	static QString strURL;
};


/***********************************************************************************************************************************/
class HttpMangerOther : public HttpManger
{
public:
    HttpMangerOther(QObject *parent = 0);
    ~HttpMangerOther();

protected:
    void initData();
};

#endif// __HTTPMANGER_H__
